<?php

// IN THE NAME OF ALL THAT IS HOLY, DON`T RUN THIS FILE UNLESS YOU NEED TO!
// THIS FILE CANNOT BE RUN WITHOUT LOCAL INTERVENTION FOR SAFETY REASONS

// Comment the next line and it`ll work
//ei5rkuj-p[3tu2q


// 2015 version.
//
// New this year:
//
//		* Tables are now flagged with amenities, so the list creator can
//		  properly remove/add proper tables and assignments don`t rely on
//		  the user remembering where these things are.
//
// TO_DO:
//		Change import system so the order_id is unique, not the dealership
//			name--this caused problems over import issues and edits.
//		Figure out why the importer mangles foreign characters.
//		Finish the serialized logger.


// All per-year values are stored in globals
require('globals.php');

	$table_validation_query_a = "DROP TABLE IF EXISTS " . $dealers_table;
	$table_validation_query_b = "DROP TABLE IF EXISTS " . $tables_table;
	$table_creation_query_a = "CREATE TABLE " . $dealers_table  . " (
		dealer_id INT(3) ZEROFILL UNSIGNED PRIMARY KEY AUTO_INCREMENT,
		order_id INT(3) ZEROFILL UNSIGNED UNIQUE,
		date_entered TEXT NOT NULL,
		dealership_name VARCHAR(255) NOT NULL,
		table_size_req INT UNSIGNED DEFAULT 0,
		table_pref INT UNSIGNED DEFAULT 0,
		wall INT UNSIGNED DEFAULT 0,
		requests TEXT,
		table_adjacent_to INT UNSIGNED DEFAULT 0,
		table_away_from INT UNSIGNED DEFAULT 0,
		table_no_request INT UNSIGNED DEFAULT 0,
		power INT UNSIGNED DEFAULT 0,
		dealer_first_name TEXT NOT NULL,
		dealer_last_name TEXT NOT NULL,
		dealer_address TEXT,
		dealer_city TEXT,
		dealer_state TEXT,
		dealer_post_code TEXT,
		dealer_country TEXT,
		dealer_email TEXT,
		dealer_phone TEXT(12),
		dealer_badge_number INT(4),
		tax_id TEXT,
		payment_method INT UNSIGNED DEFAULT 0,
		payment_amount FLOAT(5,2) UNSIGNED DEFAULT 0.00,
		is_notified INT(1),
		notification_date TEXT NOT NULL,
		cancelled INT(1),
		cancelled_date TEXT,
		trouble INT(1),
		table_note TEXT)";

	$table_creation_query_b = "CREATE TABLE " . $tables_table . "(
								row_number TEXT(5) NOT NULL,
								row_a_assignment INT(3) ZEROFILL,
								row_b_assignment INT(3) ZEROFILL,
								is_powered INT(1) ZEROFILL,
								is_wall INT(1) ZEROFILL,
								primary key (row_number(5)))";

	$table_population_query = "INSERT INTO " . $tables_table . " VALUES ";

	$row_length=20;

	$table_population_query .= "('B00C',0,0,0,0),
								('D00E',0,0,0,0),
								('K00L',0,0,0,0),
								('M00N',0,0,0,0),
								('F15G',0,0,0,0),
								('H15J',0,0,1,0),";

	foreach ($table_rows as $key)
	{
		if ($key != 'I') {
			if ($key == 'F' || $key == 'G' || $key == 'H' || $key == 'J') {
				$row_length = 20;
				$kicker = 16;
			}
			else {
				$row_length = 20;
				$kicker = 1;
			}
			for ($i=$kicker;$i<=$row_length;$i++) {
				if ($i <= 9) {
					$table_population_query .= "('" . $key . '0' . $i . "',0,0,";
				}
				else {
					$table_population_query .= "('" . $key . $i . "',0,0,";
				}
				if ((($key == 'A' || $key == 'C' || $key == 'D' || $key == 'K' || $key == 'N') && ($i >= 6 && $i <= 15))
						|| ($key == 'O' && ($i >= 6 && $i <= 11)
						|| ($key == 'J')))
					$table_population_query .= "1,";
				else
					$table_population_query .= "0,";
				if ($key == 'A' || $key == 'O') {
					$table_population_query .= "1),";
				}
				else
					$table_population_query .= "0),";
			}
		}
	}

	$table_population_query .= "('B21C',0,0,0,0),
								('D21E',0,0,0,0),
								('K21L',0,0,0,0),
								('M21N',0,0,0,0),
								('F21G',0,0,0,0),
								('H21J',0,0,1,0),
								('I01',0,0,0,0),
								('I02',0,0,0,0),
								('I03',0,0,0,0),
								('I04',0,0,0,0),
								('I05',0,0,0,0),
								('I06',0,0,0,0)"
								;

	// connect to the mysql database server.
	$connect = mysql_connect($host, $user, $password);
	if (!$connect)
		die('Could not connect: ' . mysql_error());
	else
	{
		mysql_select_db($database);
		mysql_query($table_validation_query_a);
		mysql_query($table_validation_query_b);
		if ( ! mysql_query ($table_creation_query_a))
		{
			die ( 'Could not create table  ' . $dealers_table . mysql_error() );
		}
		if ( ! mysql_query ($table_creation_query_b))
		{
			die ( 'Could not create table tables-2013: ' . $tables_table . mysql_error() );
		}
		if ( ! mysql_query ($table_population_query))
		{
			die ( 'Could not populate table tables-2013: ' . mysql_error() );
		}
		else {
			echo "Success in table creation.\n";
		}
	}
?>